﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

#endif

namespace StyleMVVM.LanguageExtensions
{
	public static class FrameworkElementExtensions
	{
		public static object TryFindResouce(this FrameworkElement element, object resourceKey)
		{
			FrameworkElement currentElement = element;

			while (currentElement != null)
			{
#if NETFX_CORE
				if (currentElement.Resources.ContainsKey(resourceKey))
#else
				if (currentElement.Resources.Contains(resourceKey))
#endif
				{
					return currentElement.Resources[resourceKey];
				}

				currentElement = VisualTreeHelper.GetParent(currentElement) as FrameworkElement;
			}

#if NETFX_CORE
			if (Application.Current.Resources.ContainsKey(resourceKey))
#else
			if (Application.Current.Resources.Contains(resourceKey))
#endif
			{
				return Application.Current.Resources[resourceKey];
			}

			return null;
		}
	}
}